﻿#
# Copyright (c) 2012 Code Owls LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy 
# of this software and associated documentation files (the "Software"), to 
# deal in the Software without restriction, including without limitation the 
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
# sell copies of the Software, and to permit persons to whom the Software is 
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
# IN THE SOFTWARE. 
#
# 	psake build script for the OData provider and module
#
# 	valid configurations:
#  		Debug
#  		Release
#
# notes:
#

properties {
	$config = 'Debug'; 	
	$local = './_local';
	$keyContainer = '';
	$slnFile = @(
		'./src/OData.sln'
	);	
    $targetPath = "./src/CodeOwls.PowerShell.OData.Provider/bin";
	$moduleSource = "./src/Modules";
    $metadataAssembly = 'CodeOwls.PowerShell.OData.Provider.dll'
};

$framework = '4.0'
$private = "this is a private task not meant for external use";

function get-packageDirectory
{
	return "." | resolve-path | join-path -child "/bin/$config";
}

function get-nugetPackageDirectory
{
    return "." | resolve-path | join-path -child "/bin/$config/NuGet";
}

function get-modulePackageDirectory
{
    return "." | resolve-path | join-path -child "/bin/$config/Modules";
}

function create-PackageDirectory( [Parameter(ValueFromPipeline=$true)]$packageDirectory )
{
    process
    {
        write-verbose "checking for package path $packageDirectory ..."
        if( !(Test-Path $packageDirectory ) )
    	{
    		Write-Verbose "creating package directory at $packageDirectory ...";
    		mkdir $packageDirectory | Out-Null;
    	}
    }    
}

function get-ProviderVersion
{
	$p = get-modulePackageDirectory;    
    $md = join-path $p "OData\bin\$metadataAssembly";
    ( get-item $md | select -exp versioninfo | select -exp productversion );
}

function get-zipPackageName
{
	"OData.$(get-ProviderVersion).zip"
}

task default -depends Install;

. ./psake/commontask.ps1

# package tasks

task PackageModule -depends CleanModule,Build,__CreateModulePackageDirectory -description "assembles module distribution file hive" -action {
	$mp = get-modulePackageDirectory;
	
	# copy module src hive to distribution hive
	Copy-Item $moduleSource -container -recurse -Destination $mp -Force;
	
	# copy bins to module bin area
	mkdir "$mp\OData\bin"
	ls "$targetPath/$config" | copy-item -dest "$mp\OData\bin" -recurse -force;
} 

task PackageZip -depends PackageModule -description "assembles zip archive of module distribution" -action {
	$mp = get-modulePackageDirectory | Get-Item;
	$pp = get-packageDirectory;
	$zp = get-zipPackageName ;
	
	Import-Module pscx;
	
	Write-Verbose "module package path: $mp";
	Write-Verbose "package path: $pp";
	Write-Verbose "zip package name: $zp";
	
	write-zip -path "$mp\OData" -outputpath $zp | Move-Item -Destination $pp -Force ;	
}

# install tasks

task Uninstall -description "uninstalls the module from the local user module repository" {
	$modulePath = $Env:PSModulePath -split ';' | select -First 1 | Join-Path -ChildPath 'OData';
	if( Test-Path $modulePath )
	{
		Write-Verbose "uninstalling module from local module repository at $modulePath";
		
		$modulePath | ri -Recurse -force;
	}
}

task Install -depends InstallModule -description "installs the module to the local machine";

task InstallModule -depends PackageModule -description "installs the module to the local user module repository" {
	$packagePath = get-modulePackageDirectory;
	$modulePath = $Env:PSModulePath -split ';' | select -First 1;
	Write-Verbose "installing module from local module repository at $modulePath";
	
	ls $packagePath | Copy-Item -recurse -Destination $modulePath -Force;	
}

